package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.BgiveRuleDao;
import com.mstore.pojo.BgiveRule;

public class BgiveRuleService {
	private BgiveRuleDao bGiveRuleDao;

	/**
	 * 保存积分规则
	 */
	public void saveBgiveRule(BgiveRule bGiveRule) {
		bGiveRuleDao.saveBgiveRule(bGiveRule);
	}

	/**
	 * 查看积分规则
	 */
	public List<BgiveRule> watchBgiveRule(BgiveRule bGiveRule) {
		List<BgiveRule> list = new ArrayList<BgiveRule>();
		List<Object> objects = bGiveRuleDao.watchBgiveRule(bGiveRule);
		for (Object object : objects) {

			BgiveRule en = (BgiveRule) object;
			if (en.getGiveValue() == null) {
				en.setCreateTime(en.getCreateTime().substring(0,
						en.getCreateTime().indexOf(".")));
				en.setFromTime(en.getFromTime().substring(0,
						en.getFromTime().indexOf(".")));
				en.setToTime(en.getToTime().substring(0,
						en.getToTime().indexOf(".")));
				list.add(en);
			} 

		}
		return list;
	}

	/**
	 * 获取一个积分规则对象
	 */
	public BgiveRule getOneBgiveRule(BgiveRule bGiveRule) {
		Object object = bGiveRuleDao.getOneBgiveRule(bGiveRule);
		if (object == null) {
			return null;
		}
		BgiveRule bg = (BgiveRule) object;
		bg.setCreateTime(bg.getCreateTime().substring(0,
				bg.getCreateTime().indexOf(".")));
		bg.setFromTime(bg.getFromTime().substring(0,
				bg.getFromTime().indexOf(".")));
		bg.setToTime(bg.getToTime()
				.substring(0, bg.getToTime().indexOf(".")));
		return bg;
	}

	/**
	 * 修改积分规则
	 */
	public void modifyBgiveRule(BgiveRule bGiveRule) {

		bGiveRuleDao.modifyBgiveRule(bGiveRule);
	}

	/**
	 * 删除积分规则
	 */
	public void delBgiveRule(BgiveRule bGiveRule) {
		bGiveRuleDao.delBgiveRule(bGiveRule);
	}

	/**
	 * 判断当前时间是否在积分活动时间内
	 */
	public List<BgiveRule> selectByTime(BgiveRule bGiveRule) {
		List<BgiveRule> list = new ArrayList<BgiveRule>();
		List<Object> objects = bGiveRuleDao.selectByTime(bGiveRule);
		BgiveRule rule = new BgiveRule();
		for (Object object : objects) {
			
			if (object instanceof String) {
				String[] obj = (String[]) object;
				for (int i = 0; i < obj.length; i++) {
					rule.setId(Long.parseLong(obj[0]));
					rule.setCreateTime(obj[1]);
					rule.setEaId(Long.parseLong(obj[2]));
					rule.setFromTime(obj[3]);
					rule.setToTime(obj[4]);
					rule.setRaiseValue(Double.parseDouble(obj[5]));
					rule.setType(Integer.parseInt(obj[6]));
					rule.setState(Integer.parseInt(obj[7]));
					list.add(rule);
				}
			}
			
		}
		return list;

	}
	/**
	 * 通过ea_id ，state来查询
	 */
	public List<BgiveRule>selectByState(BgiveRule bGiveRule){
		List<BgiveRule> list = new ArrayList<BgiveRule>();
		List<Object> objects = bGiveRuleDao.selectByState(bGiveRule);
		for (Object object : objects) {
			BgiveRule en = (BgiveRule) object;
			if (en.getGiveValue() == null) {
				en.setCreateTime(en.getCreateTime().substring(0,
						en.getCreateTime().indexOf(".")));
				en.setFromTime(en.getFromTime().substring(0,
						en.getFromTime().indexOf(".")));
				en.setToTime(en.getToTime().substring(0,
						en.getToTime().indexOf(".")));
				list.add(en);
			} 
		}
		return list;
	}
	
	public BgiveRule findBgiveRuleByType(BgiveRule bGiveRule) {
		Object object = bGiveRuleDao.findBgiveRuleByType(bGiveRule);
		return (BgiveRule)object;
	}

	/**
	 * 修改积分规则
	 */
	public void modifyGiveValue(BgiveRule bGiveRule) {
		bGiveRuleDao.modifyGiveValue(bGiveRule);
	}
	
	/**
	 * 判断当前时间是否在积分活动时间内
	 */
	public BgiveRule getGiveRuleByTime(String time, Long eaId) {
		Object obj = bGiveRuleDao.getGiveRuleByTime(time, eaId);
		return obj == null ? null : (BgiveRule) obj;
	}
	
	/**
	 * 查询注册时送积分规则
	 */
	public BgiveRule getEaGiveRule(Long eaId) {
		Object obj = bGiveRuleDao.getEaGiveRule(eaId);
		return obj == null ? null : (BgiveRule) obj;
	}

	public BgiveRuleDao getbGiveRuleDao() {
		return bGiveRuleDao;
	}

	public void setbGiveRuleDao(BgiveRuleDao bGiveRuleDao) {
		this.bGiveRuleDao = bGiveRuleDao;
	}

}
